import sun.reflect.generics.tree.Tree;

import java.util.HashSet;
import java.util.Set;

public class p653 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        root.right = new TreeNode(3);
        System.out.println(new p653().findTarget(root,6));
    }

    public boolean findTarget(TreeNode root, int k) {
        Set set = new HashSet<Integer>();
        return bfs(root,k,set);
    }

    public boolean bfs(TreeNode node,int k, Set set){
        if (node == null)
            return false;
        if(set.contains(k - node.val))
            return true;
        set.add(node.val);
        return bfs(node.left,k,set) || bfs(node.right,k,set);
    }

}
